//
//  MqFindDetailBrandViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqFindDetailBrandViewController.h"
#import "MqFindDetailView.h"
#import "PKRequestManager.h"
#import "MqBrandBikeDetailModel.h"
#import "MqBrandDetailShopsModel.h"
#import <UIImageView+WebCache.h>
#import "MqBrandShopTableViewCell.h"
#import "MqFindBikeShopDetailViewController.h"

#import "MqSingleton.h"

@interface MqFindDetailBrandViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong)NSMutableArray *shopsArray;

@property (nonatomic ,strong)MqFindDetailView  *findDetail;

@property (nonatomic ,strong)UITableView *tableView;

@property (nonatomic ,strong)UIWebView *webView;

@property (nonatomic ,assign)CGSize labelSize;

@end

@implementation MqFindDetailBrandViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.view.backgroundColor = [UIColor lightGrayColor];
    
  
    [self addTableView];
    [self setUpData];
    
    
}

//加载数据
- (void)setUpData
{
    //拼接参数
    NSString *str = [NSString stringWithFormat:@"brand_id=%@&lat=%f&lon=%f",self.brand_id,[MqSingleton shareData].lat,[MqSingleton shareData].lon];
    
//    NSLog(@"lat = %f, lon = %f", [MqSingleton shareData].lat, [MqSingleton shareData].lon);
    
    //拼接网址
    NSString *url = [NSString stringWithFormat:@"http://p.biketo.com/api/app/Brand?%@",str];
    
    //请求数据
    [PKRequestManager requestWithUrlString:url parDic:nil Method:GET finish:^(NSData *data) {
        
        //json解析
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        //用字典接受  封装到model中
        NSDictionary *oneDic = dic[@"data"][@"brand"];
        MqBrandBikeDetailModel *model = [[MqBrandBikeDetailModel alloc] init];
        [model setValuesForKeysWithDictionary:oneDic];
        
        
        //给表头上的图片 请求数据
        [_findDetail.brandPic sd_setImageWithURL:[NSURL URLWithString:model.logo] placeholderImage:[UIImage imageNamed:@"bike"]];
        
        //给表头的Label赋值
        _findDetail.nameLabel.text = model.name;
        _findDetail.countryLabel.text = [NSString stringWithFormat:@"国家:%@",model.country];
        
        _findDetail.locationLabel.text = [NSString stringWithFormat:@"定位:%@",model.niche];
        
        _findDetail.found_yearLabel.text = [NSString stringWithFormat:@"成立年份:%@",model.found_year];
        
        _findDetail.finestLabel.text = model.finest;
        

        //给descriptionLabel赋值  并且自定义高度
        _findDetail.descriptionLabel.text = model.descriPtion;
        
        [_findDetail.descriptionLabel sizeToFit];
        
        _labelSize = _findDetail.descriptionLabel.frame.size;
        
        [_findDetail.descriptionLabel setFrame:CGRectMake(10, 150, kScreenWidth - 20, _labelSize.height)];
        
        [_findDetail.view2 setFrame:CGRectMake(0, 110, kScreenWidth, _labelSize.height + 160)];
        
        [_findDetail.scrollView setFrame:CGRectMake(0, 0, kScreenWidth, _labelSize.height + 270)];
        _findDetail.scrollView.contentSize = CGSizeMake(kScreenWidth, _labelSize.height + 270);
        
        [_findDetail setFrame:CGRectMake(0, 0, kScreenWidth, _labelSize.height + 270)];
        
        
        _tableView.tableHeaderView = _findDetail;
        
       
        
        
        
        
        //接受shops数据的数组
        _shopsArray = [NSMutableArray array];
        
        NSArray *arr = dic[@"data"][@"shops"];
        for (NSDictionary *shopsDic in arr) {
            MqBrandDetailShopsModel *model = [[MqBrandDetailShopsModel alloc] init];
            [model setValuesForKeysWithDictionary:shopsDic];
            [_shopsArray addObject:model];
        }
        
        [_tableView reloadData];
    } error:^(NSError *error) {
        
    } path:@"BrandbikeDetail"];
    
}

//添加TableView方法
- (void)addTableView
{
    
    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:
        (UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
   
    
    _findDetail = [[MqFindDetailView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth,  200)];
    _tableView.tableHeaderView = _findDetail;
    
    
    [self.view addSubview:_tableView];

    
    //行高
    _tableView.rowHeight = 110;
    
    
    
    
    
}

#pragma mark -- tableview 代理方法

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _shopsArray.count;
}

//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MqBrandShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[MqBrandShopTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"Cell"];
    }
    
    MqBrandDetailShopsModel *model = _shopsArray[indexPath.row];
    cell.model  = model;
    
    //_contactButton的点击事件
    [cell.contactButton addTarget:self action:@selector(photoNumber:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //给button添加tag值
    cell.contactButton.tag = 1000 + indexPath.row;
    
    return cell;
}

//拨打电话按钮
- (void)photoNumber:(UIButton *)button
{
    //  借助webView打电话 有弹窗提示并且可以回到原来的应用程序  建议使用
    //  懒加载  因为不需要让webView显示在视图上 我创建它只是为了使用它的一个方法 如果把他添加到self.view上 会遮挡原来的视图
    MqBrandDetailShopsModel *model = _shopsArray[button.tag - 1000];
    NSString *urlString = [NSString stringWithFormat:@"tel://%@",model.contact];
    
    if (_webView == nil) {
        _webView = [[UIWebView alloc]init];
        
        NSURL *url = [NSURL URLWithString:urlString];
        //  创建一个请求
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
        //  通过请求加载webView
        [_webView loadRequest:request];
        
    } else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"该设备不支持电话功能" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
    }
}


//点击cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MqFindBikeShopDetailViewController *shopDetailVC = [[MqFindBikeShopDetailViewController alloc] init];
    MqBrandDetailShopsModel *model = _shopsArray[indexPath.row];
    shopDetailVC.shop_id = model.shop_id;
    
    [self.navigationController pushViewController:shopDetailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
